package com.chuangjiangx.member.manager.client.web.stored.controller;

import com.chuangjiangx.commons.exception.BaseException;
import com.chuangjiangx.commons.response.ResponseUtils;
import com.chuangjiangx.commons.response.UnderlineResponse;
import com.chuangjiangx.domain.shared.model.PayTerminal;
import com.chuangjiangx.member.basic.ddd.query.MerchantUserInfoQuery;
import com.chuangjiangx.member.manager.basic.web.interceptor.Login;
import com.chuangjiangx.member.manager.client.web.stored.request.MbrRefundRequest;
import com.chuangjiangx.member.manager.client.web.stored.response.MbrRefundResponse;
import com.chuangjiangx.member.manager.common.Constants;
import com.chuangjiangx.member.manager.common.ThreadLocalUser;
import com.chuangjiangx.member.stored.ddd.application.MbrRefundApplication;
import com.chuangjiangx.member.stored.ddd.application.command.MbrRefundCommand;
import com.chuangjiangx.member.stored.ddd.application.dto.MbrRefundResult;
import com.sun.jmx.snmp.ThreadContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * @author cj-ch
 * @date 2018/4/4 下午4:06
 */
@RestController
@Api(value = "消费",tags = "退款")
@RequestMapping(value = "/mbr/refund",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
public class MbrRefundClientController {

    @Autowired
    private MbrRefundApplication mbrRefundApplication;
    @Autowired
    private MerchantUserInfoQuery merchantUserInfoQuery;
    /**
     * 退款-店员
     * @param request
     * @return com.chuangjiangx.commons.response.CamelResponse
     */
    @ApiOperation(value = "退款-店员")
    @RequestMapping(value = "/refund-self",method = RequestMethod.POST)
    public UnderlineResponse<MbrRefundResponse> refundSelf(@Validated @RequestBody MbrRefundRequest request){

        return refund(request);
    }

    /**
     * 退款-店长
     * @param request
     * @return com.chuangjiangx.commons.response.CamelResponse
     */
    @RequestMapping(value = "/refund-store",method = RequestMethod.POST)
    @Login
    @ApiOperation(value = "退款-店长")
    public UnderlineResponse<MbrRefundResponse> refundStore(@Validated @RequestBody MbrRefundRequest request){
        return refund(request);
    }

    /**
     * 退款-商户
     * @param request
     * @return com.chuangjiangx.commons.response.CamelResponse
     */
    @RequestMapping(value = "/refund-all",method = RequestMethod.POST)
    @Login
    @ApiOperation(value = "退款-商户")
    public UnderlineResponse<MbrRefundResponse> refundAll(@Validated @RequestBody MbrRefundRequest request){

        return refund(request);
    }
    /**
     * 通用退款接口,支持商户/店长/店员
     * @param request
     * @return com.chuangjiangx.commons.response.CamelResponse
     */
    @RequestMapping(value = "/refund",method = RequestMethod.POST)
    @Login
    @ApiOperation(value = "退款-商户")
    public UnderlineResponse<MbrRefundResponse> refund(@Validated @RequestBody MbrRefundRequest request){
        ThreadLocalUser user = (ThreadLocalUser) ThreadContext.get(Constants.CURRENT_LOGIN_USER_KEY);
        MbrRefundResponse response = new MbrRefundResponse();
        MbrRefundCommand command = buildRefundCommand(request, user,response);
        MbrRefundResult refundResult = mbrRefundApplication.refund(command);
        BeanUtils.copyProperties(refundResult,response);
        return (UnderlineResponse<MbrRefundResponse>)ResponseUtils.success(response);
    }

    /**
     * 创建退款命令
     * @param request 退款请求
     * @return
     */
    private MbrRefundCommand buildRefundCommand(MbrRefundRequest request,ThreadLocalUser user,MbrRefundResponse response){
        /**
         * 角色类型, 0-商户  1-店长  2-店员
         */
        Integer userType = user.getUserType();
        String userPassword = merchantUserInfoQuery.queryUserPassword(user.getMerchantUserId());
        if(!Objects.equals(userPassword,DigestUtils.md5DigestAsHex(request.getPassword().getBytes()))){
            throw new BaseException("","密码错误");
        }

        MbrRefundCommand command = new MbrRefundCommand();
        command.setMerchantId(user.getMerchantId());
        command.setMerchantUserId(user.getMerchantUserId());
        command.setUserType(userType);

        command.setOrderNumber(request.getOrderNumber());
        command.setAmount(request.getAmount());
        command.setTerminal(PayTerminal.getPayTerminal(request.getTerminal()));

        /**
         * @see StoreUser.UserType
         */
        switch (userType){
            case 0:
                command.setMerchantAddress(user.getMerchantAddress());
                command.setStoreId(user.getStoreId());
                command.setStoreUserId(user.getStoreUserId());
                response.setCashierName(user.getRealName());
                break;
            case 1:
            case 2:
                command.setStoreId(user.getStoreId());
                command.setStoreUserId(user.getStoreUserId());
                command.setStoreAddress(user.getStoreAddress());

                response.setStoreName(user.getStoreName());
                response.setCashierName(user.getRealName());
                break;
            default:
                break;
        }
        return command;
    }
}
